package cn.edu.scau.cmi.houzhicong.hibernate.dao;

import cn.edu.scau.cmi.houzhicong.hibernate.domain.Student;
import cn.edu.scau.cmi.houzhicong.hibernate.domain.Teacher;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class TeacherDao {



    private static String configureFile = "/cn/edu/scau/cmi/houzhicong/hibernate/hibernate.cfg.xml";
    private static SessionFactory factory = new Configuration().configure(configureFile).buildSessionFactory();


    /* Method to CREATE an employee in the database */
    public boolean save(String id, String name){
        Session session = factory.openSession();
        Transaction tx = null;  //事务
        String teacherID = null;
        try{
            tx = session.beginTransaction();
            Teacher teacher = new Teacher(id, name);
            teacherID = (String) session.save(teacher);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
            teacherID = null;
        }finally {
            session.close();
        }
        return teacherID!=null;
    }

    public void deleteById(String id){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            Teacher teacher = (Teacher) session.get(Teacher.class, id);
            session.delete(teacher);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }



    public Teacher findById(String id){
        Session session = factory.openSession();
        Transaction tx = null;
        Teacher teacher = null;
        try{
            tx = session.beginTransaction();
            teacher = (Teacher) session.get(Teacher.class, id);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            teacher = null;
            e.printStackTrace();
        }finally {
            session.close();
        }
        return teacher;
    }



    public void update(Teacher teacher){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            session.update(teacher);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }

    public void addStudent(Teacher teacher,Student student){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            student.setTutor(teacher);
            session.update(student);
            tx.commit();
            teacher.getStudents().add(student);
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }

    public void deleteStudent(Teacher teacher,Student student){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            student.setTutor(null);
            session.update(student);
            tx.commit();
            teacher.getStudents().remove(student);
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }



}
